package com.example.home.loveDonation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.loveDonation.adapter.BenefitApter;
import com.example.home.loveDonation.bean.BenefitBean;
import com.example.home.tool.OKHttpTool;

public class BenefitActivity extends AppCompatActivity {

    private EditText search_et;
    private TextView search_btn;
    private RecyclerView rcv_list;
    private String listId;
    private int num;
    private Button last;
    private Button next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_benefit);
        Intent intent  = getIntent();
        listId = intent.getStringExtra("welf_id");
        initView();
        num = 1; // 初始化num的值
        getListData(num); // 初始加载第一页数据
    }

    private void getListData(int num) {
        OKHttpTool.doNetRequest("prod-api/api/public-welfare/public-welfare-activity/list?typeId="+listId+"&pageNum="+num+"&pageSize=5",
                OKHttpTool.GET,
                BenefitBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        BenefitBean benefitBean = (BenefitBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(benefitBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindList(BenefitBean benefitBean) {
        RecyclerView recyclerView = findViewById(R.id.rcv_list);
        BenefitApter benefitApter = new BenefitApter(BenefitActivity.this, benefitBean.getRows());
        LinearLayoutManager linearLayout= new LinearLayoutManager(BenefitActivity.this);
        recyclerView.setLayoutManager(linearLayout);
        recyclerView.setAdapter(benefitApter);


    }

    private void initView() {
        search_et = findViewById(R.id.search_et);
        search_btn = findViewById(R.id.search_btn);
        rcv_list = findViewById(R.id.rcv_list);
        last = findViewById(R.id.last);
        next = findViewById(R.id.next);

        last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num--;
                if (num < 1) {
                    num = 1;
                }
                getListData(num); // 点击上一页时重新加载数据
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num++;
                getListData(num); // 点击下一页时重新加载数据
            }
        });
    }
}
